package com.zkingsoft.services.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.MatchsDao;
import com.zkingsoft.dao.TeamDao;
import com.zkingsoft.model.Matchs;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Team;
import com.zkingsoft.services.MatchsService;


/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-05-20 20:30
 */
@Service("matchsService")
public class  MatchsServiceImpl implements    MatchsService  {

	
	@Autowired
	private MatchsDao matchsDao;
	@Autowired
	private TeamDao teamDao;
	
	@Override
	@Transactional
	public int add(Matchs matchs){
		if(matchs.getRank()==10){
			if("主队获胜".equals(matchs.getResult())){
				Team team = teamDao.selectById(matchs.getHomeId());
				if(team.getScore()!=null){
					team.setScore(team.getScore()+2);
				}else{
					team.setScore(2);
				}
				team.setScoreTime(new Date());
				teamDao.update(team);
			}else if("客队获胜".equals(matchs.getResult())){
				Team team = teamDao.selectById(matchs.getVisitingId());
				if(team.getScore()!=null){
					team.setScore(team.getScore()+2);
				}else{
					team.setScore(2);
				}
				team.setScoreTime(new Date());
				teamDao.update(team);
			}else{
				Team team1 = teamDao.selectById(matchs.getHomeId());
				Team team2 = teamDao.selectById(matchs.getVisitingId());
				if(team1.getScore()!=null){
					team1.setScore(team1.getScore()+1);
				}else{
					team1.setScore(1);
				}
				team1.setScoreTime(new Date());
				teamDao.update(team1);
				if(team2.getScore()!=null){
					team2.setScore(team2.getScore()+1);
				}else{
					team2.setScore(1);
				}
				team2.setScoreTime(new Date());
				teamDao.update(team2);
			}
		}
		return matchsDao.insert(matchs);
		
	}
   	
    @Override
	public int modify(Matchs matchs){
	
		return matchsDao.update(matchs);
	
	}
	
	@Override
	public int remove(List<Long> list){
		if(list.size()==1){
			Matchs matchs = matchsDao.selectById(list.get(0));
			if(matchs.getRank()==10){
				if("主队获胜".equals(matchs.getResult())){
					Team team = teamDao.selectById(matchs.getHomeId());
					if(team.getScore()!=null){
						team.setScore(team.getScore()-2);
					}
					team.setScoreTime(new Date());
					teamDao.update(team);
				}else if("客队获胜".equals(matchs.getResult())){
					Team team = teamDao.selectById(matchs.getVisitingId());
					if(team.getScore()!=null){
						team.setScore(team.getScore()-2);
					}
					team.setScoreTime(new Date());
					teamDao.update(team);
				}else{
					Team team1 = teamDao.selectById(matchs.getHomeId());
					Team team2 = teamDao.selectById(matchs.getVisitingId());
					if(team1.getScore()!=null){
						team1.setScore(team1.getScore()-1);
					}
					team1.setScoreTime(new Date());
					teamDao.update(team1);
					if(team2.getScore()!=null){
						team2.setScore(team2.getScore()-1);
					}
					team2.setScoreTime(new Date());
					teamDao.update(team2);
				}
			}
		}
		return matchsDao.deleteByIds(list);
	}

	@Override
	public int removeById(Long id){
	
		return matchsDao.deleteById(id);
	
	}
	
	@Override
	public List<Matchs> findInPage(Matchs matchs,  PaginationVO pageVo){
	
		return matchsDao.selectInPage(matchs , pageVo);
	
	}
	
	@Override
	public List<Matchs> findByModel(Matchs matchs){
	
		return matchsDao.selectByModel(matchs);
	
	}
	
	@Override
	public int  findTotal(Matchs matchs){
	
		return matchsDao.selectTotalRecord(matchs);
	
	}
	
	@Override
	public Matchs  findById(Long id){
	
		return matchsDao.selectById(id);
	
	}

   	
	
	
}